/***************************************************
//Filename:     stringex.c                         //
//Version:	    V1.0			                   //
//Released:		2006-11-03 14:00                   //
//Author:		Hua Weidong                        //
//
//         READ ME
**2006-11-03 14:00
1. Version V1.0 is published

****************************************************/
#include    <string.h>

#include	"generictype.h"
#include	"stringEx.h"

signed int strpos (
  const unsigned char *string,   /* string to search */
  char c)                       /* character to find */
{
    signed int ret = 0;

    while(string[ret] != c)
    {
        if (!string[ret]) return -1;
        ret++;
    }
    return ret;
}

BOOL HexStrToInt(const char *str, uint8 size, uint8 charCase, uint32 *value)
{
	uint8 i;
	*value = 0;
	for (i = 0; i < size; i++)
	{
		if (str[i] >= '0' && str[i] <= '9') 
		{
			*value = (*value) * 16 + str[i] - '0';
		}
		else if ((charCase == HEXCHAR_UPPER_CASE || charCase == HEXCHAR_ALL_CASE) && str[i] >='A' && str[i] <= 'F')
		{
			*value = (*value) * 16 + str[i] - 'A' + 10;
		}
		else if ((charCase == HEXCHAR_LOWER_CASE || charCase == HEXCHAR_ALL_CASE) && str[i] >='a' && str[i] <= 'f')
		{
			*value = (*value) * 16 + str[i] - 'a' + 10;
		}
		else
		{
			return false;
		}
	}
	return true;
}

BOOL StrToInt(const char *str, uint8 size, int32 *value)
{
	uint8 i = 0;
	*value = 0;
	if (str[0] == '-')
	{
		if (size == 1)
		{
			return false;
		}
		i++;
	}
	for ( ; i < size; i++)
	{
		if (str[i] >= '0' && str[i] <= '9') 
		{
			*value = (*value) * 10 + str[i] - '0';
		}
		else
		{
			return false;
		}
	}
	if (str[0] == '-')
	{
		*value = -(*value);
	}
	return true;
}
